package database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import main.ISpecificPath;

/**
 * This class is a singleton and gives an access to the database
 */
public class DatabaseManager
{
  /** Public instance */
  public static final DatabaseManager INSTANCE = new DatabaseManager();

  /** {@link Connection} to the database */
  private static Connection conn = null;

  /**
   * Default constructor
   */
  private DatabaseManager()
  {
    try
    {
      Class.forName("com.mysql.jdbc.Driver");
    }
    catch (ClassNotFoundException e)
    {
      e.printStackTrace();
    }
  }

  /**
   * Gets the {@link Connection} to the database
   * 
   * @return a {@link Connection}
   */
  public Connection getDatabaseConnection()
  {
    if (conn == null)
    {
      try
      {
        conn = DriverManager.getConnection(ISpecificPath.DATABASE_PATH, ISpecificPath.DATABASE_LOGIN,
            ISpecificPath.DATABASE_PASSWORD);
      }
      catch (SQLException e)
      {
        e.printStackTrace();
      }

    }
    return conn;
  }
}
